package com.gykfbxxh.imovie.utils;

import android.app.Activity;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.TranslateAnimation;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.app.utils.util.LogUtils;
import com.gykfbxxh.imovie.R;

import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by xuxinghai
 * On 2017/8/30
 */

public class AnimationUtils {

    private static AnimationUtils mInstance;

    public AnimationUtils() {
    }

    public static synchronized AnimationUtils getInstance(){
        if(mInstance == null) {
            mInstance = new AnimationUtils();
        }
        return mInstance;
    }

    /**
     * 设置自动向上滚动动画
     */
    public void autoScrollAnimation(Activity activity , final List<String> list , final RelativeLayout relativeLayout){
        this.i = 0;
        this.list = list ;
        this.activity = activity ;
        this.relativeLayout = relativeLayout;
        stopLooperTimer();
        if(relativeLayout!=null) {
            if(relativeLayout.getChildCount()>0) {
                for (int i =0;i<relativeLayout.getChildCount();i++){
                    View childAt = relativeLayout.getChildAt(i);
                    if(childAt!=null) {
                        childAt.clearAnimation();
                        childAt.setVisibility(View.GONE);
                    }
                }
            }
        }
        relativeLayout.removeAllViews();
        if(list!=null&&list.size()>0) {
            inflayouter = activity.getLayoutInflater();
            size = list.size();
            if(relativeLayout==null) {
                return;
            }
            mTimer = new Timer();
            mTimerTask = new TimerTask() {
                @Override
                public void run() {
                    mHandler.post(mUpdateResults);
                }
            };
            mTimer.schedule(mTimerTask, 1000, 1000);
        }
    }

    private LayoutInflater inflayouter;
    private Activity activity;
    private int size ;
    private List<String> list;
    private AnimationSet animationSet ;
    private RelativeLayout relativeLayout;
    int i = 0;
    /**
     * 更新文字需要在UI线程
     */
    Handler mHandler = new Handler();
    Runnable mUpdateResults = new Runnable() {
        public void run() {
            if(i>=size) {
                if(mTimer!=null) {
                    mTimer.cancel();
                    mTimer=null;
                }
                if(mTimerTask !=null) {
                    mTimerTask.cancel();
                    mTimerTask= null;
                }
                stopFlag = false;
                looperTimer();
                return;
            }
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
            params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
            TextView scrollTextView = (TextView) inflayouter.inflate(R.layout.layout_animation_scroll_text, null);
//            TextView scrollTextView = new TextView(activity);
            scrollTextView.setLayoutParams(params);
            scrollTextView.setFitsSystemWindows(true);
            scrollTextView.setText(list.get(i));
//            scrollTextView.setTextSize(10);
            relativeLayout.addView(scrollTextView);
//            scrollTextView.clearAnimation();
            scrollTextView.startAnimation(getAnimationSet());
            i++ ;
        }
    };

    private Timer mTimer = null;
    private TimerTask mTimerTask = null;
    private Timer timerLooper = null;
    private TimerTask taskLooper = null;


    private void looperTimer() {
        timerLooper = new Timer();
        taskLooper = new TimerTask() {
            @Override
            public void run() {
                //将数据源重新排序
                if(!stopFlag) {
                    LogUtils.e("xuxh","looperTimer run()");
                    mHandlerLooper.post(mUpdateResultsLooper);
                }
            }
        };
        timerLooper.schedule(taskLooper, 0, 1000);
    }

    private boolean stopFlag = false;

    public void stopLooperTimer(){
        if(mTimerTask !=null) {
            mTimerTask.cancel();
            mTimerTask= null;
        }
        if(mTimer!=null) {
            mTimer.cancel();
            mTimer=null;
        }
        if(taskLooper!=null) {
            taskLooper.cancel();
            taskLooper = null;
        }
        if(timerLooper!=null) {
            timerLooper.cancel();
            timerLooper = null;
        }
        stopFlag = true;
    }

    /**
     *  循环更新文字需要在UI线程
     */
    Handler mHandlerLooper = new Handler();
    Runnable mUpdateResultsLooper = new Runnable() {
        public void run() {
            traversalView(relativeLayout);
        }
    };

    /**
     * 遍历view的所有子控件设值，不用在创建
     * @param viewGroup
     */
    public void traversalView(final ViewGroup viewGroup) {


        if (viewGroup.getChildCount() != 0){
            mHandler.post(new Runnable() {
                @Override
                public void run() {
                    /**
                     * 此处必须这么处理，先removeview子view，解除与父类关系，再添加进去
                     * 否则会报错java.lang.IllegalStateException
                     */
                    TextView newView = (TextView) viewGroup.getChildAt(0);
                    viewGroup.removeView(viewGroup.getChildAt(0));
                    if(viewGroup==null) {
                        LogUtils.e("xuxh_looperTimer run() viewGroup = null");
                        return;
                    }
                    if(newView!=null) {
                        newView.setVisibility(View.GONE);
                        viewGroup.addView(newView);
//                            newView.clearAnimation();
                        newView.startAnimation(getAnimationSet());
                        LogUtils.e("xuxh","looperTimer run() startAnimation text="+newView.getText().toString());
                    }
                }
            });

        }
    }

    private TranslateAnimation trans1;
    private TranslateAnimation trans2;
    private TranslateAnimation trans3;
    private TranslateAnimation trans4;
    private AlphaAnimation alpha1;
    private AlphaAnimation alpha2;
    private AnimationSet getAnimationSet(){
        AnimationSet animationSet = new AnimationSet(true);
        trans1 = new TranslateAnimation(Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,0, Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,-1.1f);
        trans1.setDuration(1000);
        trans1.setFillAfter(true);
        alpha1 = new AlphaAnimation(0,1);
        alpha1.setDuration(600);
        alpha1.setFillAfter(true);
        trans2 = new TranslateAnimation(Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,0, Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,-1.1f);
        trans2.setDuration(1000);
        trans2.setStartOffset(1000);
        trans2.setFillAfter(true);
        trans3 = new TranslateAnimation(Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,0, Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,-1.1f);
        trans3.setDuration(1000);
        trans3.setStartOffset(2000);
        trans3.setFillAfter(true);
        trans4 = new TranslateAnimation(Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,0, Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,-1.1f);
        trans4.setDuration(1000);
        trans4.setStartOffset(3000);
        trans4.setFillAfter(true);
        alpha2 = new AlphaAnimation(1,0);
        alpha2.setDuration(600);
        alpha2.setStartOffset(3000);
        alpha2.setFillAfter(true);
        animationSet.addAnimation(trans1);
        animationSet.addAnimation(alpha1);
        animationSet.addAnimation(trans2);
        animationSet.addAnimation(trans3);
        animationSet.addAnimation(trans4);
        animationSet.addAnimation(alpha2);
        animationSet.setFillAfter(true);
        return animationSet;
    }

}
